/**
 * @description: 面标绘
 * @author: zhao_sk
 * @date: 2022-04-21 10:24:12
 */
export class Polygon {
	constructor(viewer) {
		this.viewer = viewer;
	}

	/**
	 * 添加面标绘
	 */
	async add(polygonData, callback) {
		const {
			id,
			locationData,
			fillColor,
			pointColor,
			pointSize,
			outline,
			outlineColor,
			outlineWidth
		} = polygonData;

		if (this.viewer.getLayer(`VGEPolygon_${id}`)) {
			return console.error('id already registered');
		}
		this.viewer.addSource(`VGEPolygon_${id}`, {
			type: 'geojson',
			// "maxzoom": 12,
			data: {
				type: 'FeatureCollection',
				features: [
					{
						type: 'Feature',
						geometry: {
							type: 'MultiPolygon',
							coordinates: [[locationData]]
						}
					}
				]
			}
		});

		this.viewer.addLayer({
			id: `VGEPolygon_${id}`,
			type: 'fill',
			source: `VGEPolygon_${id}`,
			paint: {
				'fill-color': fillColor || 'rgba(11,223,141,1)'
			}
		});

		this.viewer.addLayer({
			id: `VGEPolygon_${id}_cap`,
			type: 'circle',
			source: `VGEPolygon_${id}`,
			paint: {
				'circle-color': pointColor || '#06ceae',
				'circle-radius': +pointSize || 8
			}
		});

		if (outline) {
			this.viewer.addLayer({
				id: `VGEPolygon_${id}_line`,
				type: 'line',
				source: `VGEPolygon_${id}`,
				paint: {
					'line-color': outlineColor || '#06ceae',
					'line-width': +outlineWidth || 1
				}
			});
		}
	}

	/**
	 * 删除
	 */
	delete(id) {
		let sid = `VGEPolyline_${id}`;
		this.viewer.getLayer(sid) && this.viewer.removeLayer(sid);
		this.viewer.getLayer(`${sid}_cap`) && this.viewer.removeLayer(`${sid}_cap`);
		this.viewer.getLayer(`${sid}_line`) && this.viewer.removeLayer(`${sid}_line`);
		this.viewer.getSource(sid) && this.viewer.removeSource(sid);
		return true;
	}

	/**
	 * 清空
	 */
	clear() {
		this.viewer.getStyle().layers.forEach((item) => {
			if (
				item.id.includes('VGEPolyline_') &&
				!item.id.includes('_cap') &&
				!item.id.includes('_line')
			) {
				this.viewer.removeLayer(item.id);
				this.viewer.removeLayer(`${item.id}_cap`);
				this.viewer.removeLayer(`${item.id}_line`);
				this.viewer.removeSource(item.id);
			}
		});
		return true;
	}
}
